<?php
include(APP_PATH.'/Lib/Action/GlobalAction.class.php');

class UploadAction extends GlobalAction {

    private $deadline;

    private function checkDeadline () {
        $this->deadline = mktime(0,0,0,9,5,2013);
        if (time() > $this->deadline) {
            $this->assign('currentPage', 'paper');
            $this->display('deadline');
            exit();
        }
    }

    public function index(){
        $this->checkDeadline();
        $this->assign('currentPage', 'paper');
		$this->display();
    }
    
    public function paper() {

        $this->checkDeadline();

        if ($this->isPost()) {

            $uid = intval($_POST['uid']);
            if($uid <= 0) {
                $this->assign('stat', false);
                $this->assign('error', L('wrong-uid'));
                $this->display();
                return ;
            }
            
            $passwd = strtoupper($_POST['password']);
            
            $tblAccount = M('account');
            $ret = $tblAccount->where("uid={$uid}")->find();
            
            if (empty($ret)) {
                $this->assign('stat', false);
                $this->assign('error', L('wrong-uid'));
            } elseif ($ret['password'] != $passwd) {
                $this->assign('stat', false);
                $this->assign('error', L('wrong-passwd'));
            } 

            elseif(isset($_FILES['paper'])) {

                $tblMember = M('member');
                $memb = $tblMember->field('name')->where("uid={$uid}")->find();
                if (empty($memb)) return ;

                import('ORG.Net.UploadFile');
                $upload = new UploadFile();
                $upload->maxSize  = 52428800;   //50M
                $upload->allowExts  = explode(',', 'doc,docx,pdf,sty,bst,tex,bib,rar,zip,7z,tar,gz');
                $upload->savePath =  './Uploads/doc/';
                $upload->saveRule = time;
                $upload->uploadReplace = true;
                
                if(!$upload->upload()) {
                    $this->assign('stat', false);
                    $this->assign('error', $upload->getErrorMsg());
                }else{
                    $info = $upload->getUploadFileInfo();
                    
                    $tblPaper = M('paper');
                    $ret = $tblPaper->field('pid')->where("uid={$uid}")->find();

                    $tblPaper->create();
                    $tblPaper->uid = $uid;
                    $tblPaper->filename = $info[0]['name'];
                    $tblPaper->savename = $info[0]['savename'];
                    $tblPaper->createtime = time();

                    if ($ret) {
                        $tblPaper->pid = $ret['pid'];
                        $ret = $tblPaper->save();
                    } else {
                        $ret = $tblPaper->add();
                    }
                    
                    if ($ret) {
                        $this->assign('stat', true);
                    } else {
                        $this->assign('stat', false);
                        $this->assign('error', 'Error occurred when writing data into database');
                    }
                }
            }
 
/*            
            elseif (isset($_POST['abstract'])) {
                
                $abstract = strip_tags($_POST['abstract'], '<p><br><ul><ol><li>');
                
                $tblAbstract = M('abstract');

                $ret = $tblAbstract->field('aid')->where("uid={$uid}")->find();

                $tblAbstract->create();
                $tblAbstract->uid = $uid;
                $tblAbstract->abstract = $abstract;

                if ($ret) {
                    $tblAbstract->aid = $ret['aid'];
                    $ret = $tblAbstract->save();
                } else {
                    $tblAbstract->createtime = time();
                    $ret = $tblAbstract->add();
                }

                if ($ret) {
                    $this->assign('stat', true);
                } else {
                    $this->assign('stat', false);
                    $this->assign('error', 'Error occurred when writing data into database');
                }
            }
*/
            else {
                $this->assign('stat', false);
                $this->assign('error', 'Files don\'t exist');
            }
        }
        $this->display();
    }
}